package com.tupu.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tupu.common.Result;
import com.tupu.entity.admin.LabelInfo;
import com.tupu.entity.admin.LabelPropertyInfo;
import com.tupu.entity.admin.LabelVisualized;
import com.tupu.entity.admin.LabelVisualizedParam;
import com.tupu.service.admin.LabelService;
import com.tupu.service.admin.PropertyService;
import com.tupu.service.admin.RelationBuildingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 知识管理 本体管理
 */
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/api/graph")
public class LabelController {

    @Autowired(required = false)
    private LabelService labelService;

    @Autowired(required = false)
    private PropertyService propertyService;

    @Autowired(required = false)
    private RelationBuildingService relationBuildingService;

    /**
     * 知识构建首页 标签列表，根据node_id关联首页图谱列表
     *
     * @return
     */
    @PostMapping("/list")
    public Result<List<LabelInfo>> findByNodeId(@RequestParam(required = false) Long node_id) {
        try {
            List<LabelInfo> nodeLists = labelService.listLabel(node_id);
            return Result.ok(nodeLists);
        } catch (Exception e) {
            return Result.errorMessage("查询失败：" + e.getMessage());
        }
    }

    /**
     * 知识构建首页 标签列表，根据node_id关联首页图谱列表 标签模糊搜索,分页
     *
     * @return
     */
    @PostMapping("/listSearch")
    public Result searchByNodeId(@RequestParam(required = false) Long node_id, @RequestParam(required = false) String label, @RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "10") Integer size) {
        try {
            Page<LabelInfo> infoPage = labelService.pageCondition(node_id, label, page, size);
            return Result.ok(infoPage);
        } catch (Exception e) {
            return Result.errorMessage("错误信息:" + e.getMessage());
        }
    }

    /**
     * 知识构建首页 所有标签
     *
     * @return
     */
    @PostMapping("/listAll")
    public Result<List<LabelInfo>> findAll() {
        try {
            List<LabelInfo> nodeLists = labelService.list(null);
            return Result.ok(nodeLists);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 知识构建 通过标签id查找一条标签详情
     *
     * @param id
     * @return
     */
    @PostMapping("/listOne")
    public Result findOne(@RequestParam(required = false) Long id) {
        try {
            LabelInfo labelList = labelService.findOne(id);
            return Result.ok(labelList);
        } catch (Exception e) {
            return Result.errorMessage("错误信息：" + e.getMessage());
        }
    }

    /**
     * 新增标签,属性
     *
     * @param
     * @param
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody(required = false) LabelInfo label) {
        try {
            labelService.addLabel(label);
            return Result.ok("添加成功");
        } catch (Exception e) {
            return Result.errorMessage("新增失败,错误信息:" + e.getMessage());
        }
    }

    /**
     * 根据id更新标签及属性+实例、实例关系
     *
     * @param label
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody(required = false) LabelInfo label) {
        try {
            labelService.updateLabelAndAttrsAndNode(label);
            return Result.ok("编辑成功");
        } catch (Exception e) {
            return Result.errorMessage("修改失败,错误信息:" + e.getMessage());
        }
    }

    /**
     * 删除标签，标签属性，关系关联表，节点，节点关系
     *
     * @param label_id
     * @param node_id
     * @return
     */
    @PostMapping("/del")
    public Result del(@RequestParam(required = false) Long label_id, @RequestParam(required = false) Long node_id) {
        try {
            labelService.del(label_id, node_id);
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.errorMessage("删除标签失败,错误信息:" + e.getMessage());
        }
    }

    /**
     * 根据本体名字搜索
     *
     * @return
     */
    @PostMapping("findByLabel")
    public Result<List<LabelInfo>> findByName(@RequestParam(required = false) String label) {
        try {
            List<LabelInfo> list = labelService.findByName(label);
            return Result.ok(list);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 通过本体id（label_id）查询关系数据
     *
     * @param ids
     * @return
     */
    @PostMapping("/findById")
    public Result<List<LabelInfo>> findList(@RequestParam(required = false, value = "id") List<Integer> ids) {
        try {
            List<LabelInfo> labelLists = labelService.findList(ids);
            return Result.ok(labelLists);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 增加本体,属性 可视化接口
     *
     * @param
     * @param
     * @return
     */
    @PostMapping("/addVis")
    public Result addVis(@RequestBody(required = false) LabelInfo label) {
        try {
            LabelVisualized labelVis = labelService.addLabelVis(label);
            return Result.ok(labelVis);
        } catch (Exception e) {
            return Result.errorMessage("新建失败，请重试:" + e.getMessage());
        }
    }

    /**
     * 知识构建首页 标签列表，根据node_id关联首页图谱列表 可视化
     *
     * @param node_id
     * @return
     */
    @PostMapping("/listVis")
    public Result<LabelVisualized> findByNodeIdVis(@RequestParam(required = false) Long node_id) {
        try {
            LabelVisualized lv = labelService.findByNodeIdVis(node_id);
            return Result.ok(lv);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 根据label_id查询所有属性
     *
     * @param label_id
     * @return
     */
    @PostMapping("/findAttrsByLabelId")
    public Result<List<LabelPropertyInfo>> findAttrsById(@RequestParam(required = false) Long label_id) {
        try {
            List<LabelPropertyInfo> labelPropertyInfos = propertyService.findByLabelId(label_id);
            return Result.ok(labelPropertyInfos);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 根据实例id 查询标签下所有属性
     *
     * @param case_id
     * @return
     */
    @PostMapping("/findAttrsByCaseId")
    public Result<List<LabelPropertyInfo>> findAttrsByName(@RequestParam(required = false) Long case_id) {
        try {
            List<LabelPropertyInfo> labelPropertyInfos = labelService.findAttrsByName(case_id);
            return Result.ok(labelPropertyInfos);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 根据pro_id查询属性
     *
     * @param pro_id
     * @return
     */
    @PostMapping("/findAttrByProId")
    public Result<LabelPropertyInfo> findAttrByProId(@RequestParam(required = false) Integer pro_id) {
        try {
            LabelPropertyInfo property = propertyService.getById(pro_id);
            return Result.ok(property);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 根据id 集合删除本体 及属性
     *
     * @param ids
     * @return
     */
    @PostMapping("/delByIds")
    public Result delByIds(@RequestParam(required = false) List<Long> ids) {
        try {
            labelService.delByIds(ids);
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.errorMessage("删除错误:" + e.getMessage());
        }
    }

    /**
     * 修改标签颜色参数
     *
     * @param labelParam
     * @return
     */
    @PostMapping("/updateColor")
    public Result updateLabelColor(@RequestBody(required = false) LabelVisualizedParam labelParam) {
        try {
            labelService.updateLabelColor(labelParam);
            return Result.ok();
        } catch (Exception e) {
            return Result.errorMessage("错误信息:" + e.getMessage());
        }
    }

    /**
     * 标签关系搜索 可视化
     *
     * @param node_id
     * @param relation
     * @return
     */
    @PostMapping("/relationSearch")
    public Result relationSearch(@RequestParam(required = false) Long node_id, @RequestParam(required = false) String relation) {
        try {
            LabelVisualized Vis = relationBuildingService.relationSearch(node_id, relation);
            return Result.ok(Vis);
        } catch (Exception e) {
            return Result.errorMessage("错误信息:" + e.getMessage());
        }
    }
}